package org.uim.cricket.storage.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.HashMap;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;

import org.uim.cricket.db.impl.DBDataSetResultQuery;
import org.uim.cricket.entities.FullGround;

/**
 *
 * @author Isuru Madushanka
 */
public class AddNewGround extends javax.swing.JDialog {

	private static final long serialVersionUID = 3118483237557701326L;
	private String theContinent = null;
	private String displayName = null;
	private FullGround ground = null;
	private HashMap<String, Integer> continents = null;
	
    /** Creates new form AddNewGround */
    public AddNewGround(java.awt.Frame parent, boolean modal, FullGround fg, HashMap<String, Integer> continents) {
        super(parent, modal);
        
        this.ground = fg;
        this.continents = continents;
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        initComponents();
        
        this.setCenterScreen();
        this.fillComponents();
    }

    public FullGround getFullGround() {
    	return this.ground;
    }
    
    public String getDisplayName() {
    	return this.displayName;
    }
    
    private void setCenterScreen() {
        Dimension window = this.getSize();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        int newX = (int) (screen.getWidth() - window.getWidth()) / 2;
        int newY = (int) (screen.getHeight() - window.getHeight()) / 2;

        this.setLocation(newX, newY);
    }

    private void fillComponents() {
        this.lblID.setText(ground.getId());
        this.lblFullName.setText(ground.getFullName());
        this.lblShortName.setText(ground.getShortName());
        this.txtDisplayName.setText(ground.getTown());
        this.lblTown.setText(ground.getTown());
        this.txtCountry.setText(ground.getCountry());
        
        String cnt = null;
        try {
        	String q = "CALL sp_Get_Continent('" + ground.getCountry() + "')";
        	DBDataSetResultQuery dbr = new DBDataSetResultQuery();
        	String[][] dt = dbr.result(q, 1);
        	if (dt != null && dt.length > 0) {
        		cnt = dt[0][0];
        	}
        } catch (Exception ex) {
        	
        }
        
        this.setTitle("New Ground - #" + this.lblID.getText() + " - " + this.lblFullName.getText());

        DefaultComboBoxModel cmbModel = new DefaultComboBoxModel();
        int sindex = -1; 
        int curr = 0;
        if (this.continents != null) {
        	for (String cont : this.continents.keySet()) {
        		cmbModel.addElement(cont);
        		if (sindex == -1 && cnt != null && cont.equals(cnt)) sindex = curr;
        		curr++;
        	}
        }
        this.cmbContinent.setModel(cmbModel);
        if (sindex >= 0) this.cmbContinent.setSelectedIndex(sindex);
        
        DefaultListModel lstModel = new DefaultListModel();
        if (this.ground.getHomeTeams() != null) {
        	for (String team : this.ground.getHomeTeams()) {
        		lstModel.addElement(team);
        	}
        }
        this.lstHomeTeams.setModel(lstModel);
        this.lblHomeTeams.setText("Home Teams : (" + lstModel.size() + ")");

        lstModel = new DefaultListModel();
        if (this.ground.getAttribs() != null) {
        	for (String attr : this.ground.getAttribs().keySet()) {
        		lstModel.addElement(attr + ": " + this.ground.getAttribs().get(attr));
        	}
        }
        this.lstAttributes.setModel(lstModel);
        this.lblAttrs.setText("Attributes : (" + lstModel.size() + ")");
    }

    public String getTheContinent() {
        return theContinent;
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCountry = new javax.swing.JTextField();
        cmbContinent = new javax.swing.JComboBox();
        lblTown = new javax.swing.JLabel();
        lblShortName = new javax.swing.JLabel();
        lblFullName = new javax.swing.JLabel();
        lblID = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtDisplayName = new javax.swing.JTextField();
        lblHomeTeams = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstHomeTeams = new javax.swing.JList();
        lblAttrs = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstAttributes = new javax.swing.JList();
        cmdOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("ID:");

        jLabel2.setText("Full name:");

        jLabel3.setText("Short name:");

        jLabel4.setText("Country:");

        jLabel5.setText("Town:");

        jLabel6.setText("Continent:");

        cmbContinent.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblTown.setText("A");

        lblShortName.setText("Sn");

        lblFullName.setText("Fn");

        lblID.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblID.setText("id");

        jLabel7.setText("Display Name:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFullName)
                            .addComponent(lblShortName)
                            .addComponent(lblID))
                        .addGap(5, 5, 5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbContinent, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCountry, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                            .addComponent(lblTown)
                            .addComponent(txtDisplayName, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblFullName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblShortName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtDisplayName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblTown))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cmbContinent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );

        lblHomeTeams.setText("Home Teams:");

        jScrollPane1.setViewportView(lstHomeTeams);

        lblAttrs.setText("Attributes:");

        jScrollPane2.setViewportView(lstAttributes);

        cmdOK.setText("OK");
        cmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                    .addComponent(lblHomeTeams, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAttrs, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                    .addComponent(cmdOK, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblHomeTeams)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAttrs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmdOK)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {
        String country = ground.getCountry();
        this.displayName = this.txtDisplayName.getText();
        this.theContinent = this.cmbContinent.getSelectedItem().toString();
        if (!this.txtCountry.getText().equals(country)) {
            ground.setCountry(this.txtCountry.getText());
        }

        this.setVisible(false);
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddNewGround dialog = new AddNewGround(new javax.swing.JFrame(), true, new FullGround(), null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JComboBox cmbContinent;
    private javax.swing.JButton cmdOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAttrs;
    private javax.swing.JLabel lblFullName;
    private javax.swing.JLabel lblHomeTeams;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblShortName;
    private javax.swing.JLabel lblTown;
    private javax.swing.JList lstAttributes;
    private javax.swing.JList lstHomeTeams;
    private javax.swing.JTextField txtCountry;
    private javax.swing.JTextField txtDisplayName;
    // End of variables declaration

}
